/* cLevel.h
 * SMW Level.
 */
#ifndef __newmoon_clevel_h
#define __newmoon_clevel_h

class Level;
#include <list>
#include <string>
#include <gtk/gtk.h>
#include <string.h> //memset
#include "debugout.h"
#include "compat.h"
#include "mathx.h"
#include "uint.h"
#include "gui/cDrawBox.h"
#include "cSNESPointer.h"
#include "cSMWObject.h"
#include "cROMFile.h"
#include "cGFXBuf.h"
#include "smw.h"

class Level {
	public:
		friend class SMWObject;
		SMWLevelHeader Header;
		std::list<SMWObject> Layer1Obj, Layer2Obj;
		bool Layer2Objects; //Level has objects on layer 2
		u16 ScreenExit[MAX_SCREENS]; //Screen exit destination for each screen; 0 for none
		//Note that the game does not clear this table between levels
		GFXBuf GFX, AnimatedGFX;
		u16 Palette[16][16];
		u16 AnimPalette[16][16][16]; //Array of colours for each frame. 0 means
		//don't change the colour.
		u16 BGColour;
		/* These are the Map16 tile data read from ROM. Each element defines one
		 * 8x8 tile, so to look up a Map16 tile, multiply its index by 4. The
		 * tiles are top left, bottom left, top right, bottom right.
		 */
		u16 Map16Tile[2048], BGMap16Tile[2048];
		
		Level();
		Level(ROMFile *File);
		Level(ROMFile *File, u16 Num);
		~Level();
		void UseFile(ROMFile *File);
		bool Load(u16 Num);
		void LoadGraphics();
		void DrawAnimationFrame(u32 Frame);
		SNESPointer GetGraphicsFileAddr(u8 ID);
		void LoadGraphicsFile(u8 ID, u32 Tile);
		void LoadSharedPalettes();
		void LoadPartialPalette(u32 Idx, u32 Base, u32 PStart, u32 PEnd, u32 CStart, u32 CEnd);
		void LoadObjPalettes();
		void LoadSpritePalettes();
		void LoadBGPalettes();
		void LoadAnimatedPalettes();
		void LoadBGColour();
		bool ReadMap16();
		void Render(ImgBuf *Buf, u32 ScrollX, u32 ScrollY);
		u16 GetNum();
		void SetTileAt(u32 X, u32 Y, u16 Tile);
		u16 GetTileAt(u32 X, u32 Y);
		SMWObject* GetObjectAt(u32 X, u32 Y);
		u16 GetColour(u8 Palette, u8 Colour);
		u16 GetBGColour();
		void Unload();
	
	protected:
		ROMFile *File; //ROM file to read from
		u16 LevelNum; //Which level is currently loaded (0xFFFF = none)
		u16 Map16[16*27*MAX_SCREENS]; //Width of screen * height of screen * # screens
	
	private:
		void _Create(ROMFile *File, u16 Num);
};
#endif //__newmoon_clevel_h
